package com.zhongyewx.teachercert.ui.activity;

import android.annotation.SuppressLint;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.constraint.ConstraintLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.zhongyewx.teachercert.R;
import com.zhongyewx.teachercert.api.ZYARouterApi;
import com.zhongyewx.teachercert.base.ZYBaseActivity;
import com.zhongyewx.teachercert.db.ZYTCDataBase;
import com.zhongyewx.teachercert.framework.glide.ZYGlideUtil;
import com.zhongyewx.teachercert.framework.skin.ZYSkinFactory;
import com.zhongyewx.teachercert.utils.ZYSharedPreferencesUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


/**
 * A login screen that offers login via email/password.
 */
@Route(path = ZYARouterApi.AROUTER_LOGIN_ACTY, extras = ZYARouterApi.AROUTER_NONEED_LOGIN)
public class ZYLoginActivity extends ZYBaseActivity {


    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.layout_login)
    ConstraintLayout layoutLogin;
    @BindView(R.id.iv_login)
    ImageView ivLogin;
    @BindView(R.id.tv_login_Content)
    TextView tvLoginContent;
    private ZYSkinFactory skinFactory;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login_layout;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
//        SnackbarUtil.showSnackbar(tvLogin, "哈哈哈哈", "点我试一下", new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ZYToastUtil.showCenterToast(ZYLoginActivity.this, "hahah");
//            }
//        });
        changeTextATTR(R.color.white, R.color.color_999999, R.dimen.text_size_small);

    }

    @Override
    public void initData() {
        ZYGlideUtil.LoadImage(this, "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3714708735,3692223904&fm=27&gp=0.jpg", ivLogin);
    }


    @Override
    public void request() {
        ZYTCDataBase.instance(this).getUserDao()
            .loadUsersSync()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe();

    }

    private int change = 1;
    @OnClick(R.id.iv_login)
    public void onViewClicked() {
        if (change == 1) {
            changeTextATTR(R.color.white, R.color.color_666666, R.dimen.text_size_normal);
            change++;
        } else if (change == 2) {
            changeTextATTR(R.color.orange, R.color.color_333333, R.dimen.text_size_moddle);
            change ++;
        } else if (change == 3) {
            changeTextATTR(R.color.color_ff9464, R.color.color_008577, R.dimen.text_size_big);
            change ++;
        } else if (change == 4) {
//            changeTextATTR(R.color.red, R.color.color_008577, R.dimen.text_size_hurge);
//            change = 1;
            ZYSharedPreferencesUtils.putBooleanValue("isLogin", true);
            aRouterTurnLogin(ZYARouterApi.AROUTER_MAIN_ACTY);
            ARouter.getInstance().build(ZYARouterApi.AROUTER_MAIN_ACTY).navigation(this, new NavCallback() {
                @Override
                public void onArrival(Postcard postcard) {
                    finish();
                }

                @Override
                public void onInterrupt(Postcard postcard) {
                    super.onInterrupt(postcard);
                }
            });
        }
    }

    private void changeTextATTR(@ColorRes int bgColor, @ColorRes int color, @DimenRes int size){
        if (skinFactory == null) {
            skinFactory = new ZYSkinFactory();
        }
        skinFactory.collectViewAttr(layoutLogin, this, ZYSkinFactory.ATTR_NAME_BG, ZYSkinFactory.ATTR_TYPE_COLOR, bgColor);
        skinFactory.collectViewAttr(tvLogin, this, ZYSkinFactory.ATTR_NAME_TEXT, ZYSkinFactory.ATTR_TYPE_COLOR, color);
        skinFactory.collectViewAttr(tvLogin,this,ZYSkinFactory.ATTR_NAME_TEXT, ZYSkinFactory.ATTR_TYPE_SIZE, size);
        skinFactory.collectViewAttr(tvLoginContent,this, ZYSkinFactory.ATTR_NAME_TEXT, ZYSkinFactory.ATTR_TYPE_COLOR, color);
        skinFactory.collectViewAttr(tvLoginContent,this,ZYSkinFactory.ATTR_NAME_TEXT, ZYSkinFactory.ATTR_TYPE_SIZE, size);
    }
}

